//
//  ZGWJiBingFengXianJiuZhengFourthTableViewCell.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/6/7.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ZGWJiBingFengXianJiuZhengFourthTableViewCell.h"

@implementation ZGWJiBingFengXianJiuZhengFourthTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

// 模拟数据初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withJiBingShiNameArray:(NSMutableArray *)array jiBingShiDic:(NSMutableDictionary *)dic
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.jiBingNameArray = [NSMutableArray arrayWithArray:array];
        [self addMySubviewWithJiBingShiNameArray:array jiBingShiDic:dic];
    }
    return self;
}

// 真实数据初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withZhenJiBingShiNameArray:(NSMutableArray *)array jiBingShiDic:(NSMutableDictionary *)dic
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.jiBingNameArray = [NSMutableArray arrayWithArray:array];
        [self zhenAddMySubviewWithJiBingShiNameArray:array jiBingShiDic:dic];
    }
    return self;
}

#pragma mark - 模拟数据布局
- (void)addMySubviewWithJiBingShiNameArray:(NSMutableArray *)jiBingShiNameArray jiBingShiDic:(NSMutableDictionary *)jiBingShiDic
{
    // 记录下一个疾病开始的位置
    CGFloat nextJiBingStartY = 0.0;
    for (int i = 0; i < jiBingShiNameArray.count; i++) {
        UILabel *jiBingNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(25 * kAdjustParamater, 10 * kAdjustParamater + nextJiBingStartY, 70 * kAdjustParamater, 20 * kAdjustParamater)];
        //   shengHuoXiGuanNameLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        jiBingNameLabel.text = jiBingShiNameArray[i];
        jiBingNameLabel.textColor = [UIColor blackColor];
        jiBingNameLabel.textAlignment = NSTextAlignmentLeft;
        jiBingNameLabel.font = [UIFont systemFontOfSize:k17Big];
        jiBingNameLabel.tag = 500 + i;
        jiBingNameLabel.numberOfLines = 0;
        // 自适应高度
        CGRect newJiBingLabel = jiBingNameLabel.frame;
        newJiBingLabel.size.height = [[NetStatusHandle shareHandle] adjustHeight:jiBingNameLabel.text size:17 width:70];
        jiBingNameLabel.frame = newJiBingLabel;
        
        nextJiBingStartY = nextJiBingStartY + jiBingNameLabel.height + 25 * kAdjustParamater;
        [self.contentView addSubview:jiBingNameLabel];
        
        // 严重前方的button
        UIButton *yanZhongButton = [UIButton buttonWithType:UIButtonTypeSystem];
        yanZhongButton.frame = CGRectMake(125 * kAdjustParamater, jiBingNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        yanZhongButton.layer.cornerRadius = yanZhongButton.width / 2;
        yanZhongButton.layer.masksToBounds = YES;
        yanZhongButton.tag = 100 + i;
        [yanZhongButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:yanZhongButton];
        
        // 严重label
        UILabel *yanZhongLabel = [[UILabel alloc] initWithFrame:CGRectMake(yanZhongButton.right + 10 * kAdjustParamater, jiBingNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        yanZhongLabel.text = @"严重";
        yanZhongLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:yanZhongLabel];
        
        // 轻微前方的button
        UIButton *qingWeiButton = [UIButton buttonWithType:UIButtonTypeSystem];
        qingWeiButton.frame = CGRectMake(yanZhongLabel.right + 17 * kAdjustParamater, jiBingNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        qingWeiButton.layer.cornerRadius = qingWeiButton.width / 2;
        qingWeiButton.layer.masksToBounds = YES;
        qingWeiButton.tag = 200 + i;
        [qingWeiButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:qingWeiButton];
        
        // 轻微label
        UILabel *qingWeiLabel = [[UILabel alloc] initWithFrame:CGRectMake(qingWeiButton.right + 10 * kAdjustParamater, jiBingNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        qingWeiLabel.text = @"轻微";
        qingWeiLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:qingWeiLabel];
        
        // 无前方的button
        UIButton *wuButton = [UIButton buttonWithType:UIButtonTypeSystem];
        wuButton.frame = CGRectMake(qingWeiLabel.right + 17 * kAdjustParamater, jiBingNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        wuButton.layer.cornerRadius = wuButton.width / 2;
        wuButton.layer.masksToBounds = YES;
        wuButton.tag = 300 + i;
        [wuButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:wuButton];
        
        // 无label
        UILabel *wuLabel = [[UILabel alloc] initWithFrame:CGRectMake(wuButton.right + 10 * kAdjustParamater, jiBingNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        wuLabel.text = @"无";
        wuLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:wuLabel];
        
        // 获取对应疾病的数值
        NSString *jiBingCount = [jiBingShiDic valueForKey:jiBingShiNameArray[i]];
        if ([jiBingCount isEqualToString:@"1"]) {
            // 严重
            [yanZhongButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            [qingWeiButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            yanZhongLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
            qingWeiLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        } else if ([jiBingCount isEqualToString:@"2"]) {
            // 轻微
            [yanZhongButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [qingWeiButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            yanZhongLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            qingWeiLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        } else if ([jiBingCount isEqualToString:@"3"]) {
            // 无
            [yanZhongButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [qingWeiButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            yanZhongLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            qingWeiLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
        }

    }
    // 获取最后一个疾病nameLabel
    UILabel *lastJiBingNameLabel = (UILabel *)[self.contentView viewWithTag:499 + jiBingShiNameArray.count];
    self.bottomGrayLine = [[UILabel alloc] initWithFrame:CGRectMake(0, lastJiBingNameLabel.bottom + 30 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    self.bottomGrayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.07];
    [self.contentView addSubview:_bottomGrayLine];
    
}

#pragma mark - 真实数据布局
- (void)zhenAddMySubviewWithJiBingShiNameArray:(NSMutableArray *)jiBingShiNameArray jiBingShiDic:(NSMutableDictionary *)jiBingShiDic
{
    // 记录下一个疾病开始的位置
    CGFloat nextJiBingStartY = 0.0;
    for (int i = 0; i < jiBingShiNameArray.count; i++) {
        UILabel *jiBingNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(25 * kAdjustParamater, 10 * kAdjustParamater + nextJiBingStartY, 70 * kAdjustParamater, 20 * kAdjustParamater)];
        //   shengHuoXiGuanNameLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        jiBingNameLabel.text = jiBingShiNameArray[i];
        jiBingNameLabel.textColor = [UIColor blackColor];
        jiBingNameLabel.textAlignment = NSTextAlignmentLeft;
        jiBingNameLabel.font = [UIFont systemFontOfSize:k17Big];
        jiBingNameLabel.tag = 500 + i;
        jiBingNameLabel.numberOfLines = 0;
        // 自适应高度
        CGRect newJiBingLabel = jiBingNameLabel.frame;
        newJiBingLabel.size.height = [[NetStatusHandle shareHandle] adjustHeight:jiBingNameLabel.text size:17 width:70];
        jiBingNameLabel.frame = newJiBingLabel;
        
        nextJiBingStartY = nextJiBingStartY + jiBingNameLabel.height + 25 * kAdjustParamater;
        [self.contentView addSubview:jiBingNameLabel];
        
        // 严重前方的button
        UIButton *yanZhongButton = [UIButton buttonWithType:UIButtonTypeSystem];
        yanZhongButton.frame = CGRectMake(125 * kAdjustParamater, jiBingNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        yanZhongButton.layer.cornerRadius = yanZhongButton.width / 2;
        yanZhongButton.layer.masksToBounds = YES;
        yanZhongButton.tag = 100 + i;
        [yanZhongButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:yanZhongButton];
        
        // 严重label
        UILabel *yanZhongLabel = [[UILabel alloc] initWithFrame:CGRectMake(yanZhongButton.right + 10 * kAdjustParamater, jiBingNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        yanZhongLabel.text = @"严重";
        yanZhongLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:yanZhongLabel];
        
        // 轻微前方的button
        UIButton *qingWeiButton = [UIButton buttonWithType:UIButtonTypeSystem];
        qingWeiButton.frame = CGRectMake(yanZhongLabel.right + 17 * kAdjustParamater, jiBingNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        qingWeiButton.layer.cornerRadius = qingWeiButton.width / 2;
        qingWeiButton.layer.masksToBounds = YES;
        qingWeiButton.tag = 200 + i;
        [qingWeiButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:qingWeiButton];
        
        // 轻微label
        UILabel *qingWeiLabel = [[UILabel alloc] initWithFrame:CGRectMake(qingWeiButton.right + 10 * kAdjustParamater, jiBingNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        qingWeiLabel.text = @"轻微";
        qingWeiLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:qingWeiLabel];
        
        // 无前方的button
        UIButton *wuButton = [UIButton buttonWithType:UIButtonTypeSystem];
        wuButton.frame = CGRectMake(qingWeiLabel.right + 17 * kAdjustParamater, jiBingNameLabel.top + 2 * kAdjustParamater, 16 * kAdjustParamater, 16 * kAdjustParamater);
        wuButton.layer.cornerRadius = wuButton.width / 2;
        wuButton.layer.masksToBounds = YES;
        wuButton.tag = 300 + i;
        [wuButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:wuButton];
        
        // 无label
        UILabel *wuLabel = [[UILabel alloc] initWithFrame:CGRectMake(wuButton.right + 10 * kAdjustParamater, jiBingNameLabel.top, 35 * kAdjustParamater, 20 * kAdjustParamater)];
        wuLabel.text = @"无";
        wuLabel.font = [UIFont systemFontOfSize:k17Big];
        [self.contentView addSubview:wuLabel];
        
        // 获取对应疾病的数值
        NSString *jiBingCount = [jiBingShiDic valueForKey:jiBingShiNameArray[i]];
        if ([jiBingCount isEqualToString:@"1"]) {
            // 严重
            [yanZhongButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            [qingWeiButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            yanZhongLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
            qingWeiLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        } else if ([jiBingCount isEqualToString:@"2"]) {
            // 轻微
            [yanZhongButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [qingWeiButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            yanZhongLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            qingWeiLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        } else if ([jiBingCount isEqualToString:@"3"]) {
            // 无
            [yanZhongButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [qingWeiButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"xuanzhong-jibingjiuzheng"] forState:UIControlStateNormal];
            yanZhongLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            qingWeiLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:1];
        } else if ([jiBingCount isEqualToString:@"-1"]) {
            // 未选
            [yanZhongButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [qingWeiButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            [wuButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong-jibinghfengxian"] forState:UIControlStateNormal];
            yanZhongLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            qingWeiLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            wuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        }
        
    }
    // 获取最后一个疾病nameLabel
    UILabel *lastJiBingNameLabel = (UILabel *)[self.contentView viewWithTag:499 + jiBingShiNameArray.count];
    self.bottomGrayLine = [[UILabel alloc] initWithFrame:CGRectMake(0, lastJiBingNameLabel.bottom + 30 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    self.bottomGrayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.07];
    [self.contentView addSubview:_bottomGrayLine];
    
}


#pragma mark - 点击按钮
- (void)click:(UIButton *)button
{
  //  NSLog(@"button.tag = %ld", (long)button.tag);
    NSInteger row = 0;
    if (button.tag >=100 && button.tag < 200) {
        // 说明点击的严重
        row = button.tag - 100;
    } else if (button.tag >=200 && button.tag < 300) {
        // 说明点击的轻微
        row = button.tag - 200;
    } else if (button.tag >=300) {
        // 说明点击的无
        row = button.tag - 300;
    }
    // 疾病的名字
    NSString *jiBingName = self.jiBingNameArray[row];
    if ([self.delegate respondsToSelector:@selector(clickTag:withJiBingName:)]) {
        [self.delegate clickTag:button.tag withJiBingName:jiBingName];
    }
}

@end
